package com.sbhotel.chotellauncher.activity;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.sbhotel.chotellauncher.R;
import com.sbhotel.chotellauncher.adapter.AllAppAdapter;
import com.sbhotel.chotellauncher.entity.AppBean;
import com.sbhotel.chotellauncher.widget.HorizontalRecyclerView;
import com.sbhotel.chotellauncher.widget.VerticalRecyclerView;

import java.util.ArrayList;
import java.util.List;

public class AllAppActivity extends BaseActivity implements AllAppAdapter.OnItemClickLitener{

    private HorizontalRecyclerView horizontalRecyclerView;
    private VerticalRecyclerView verticalRecyclerView;
    private AllAppAdapter appAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {
        initTitleBar("全部应用", "");

        appAdapter = new AllAppAdapter(this, getAllApp());
        /*horizontalRecyclerView = (HorizontalRecyclerView) findViewById(R.id.horizontalRecyclerView);
        horizontalRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(3, OrientationHelper.HORIZONTAL));
        horizontalRecyclerView.setAdapter(appAdapter);*/

        verticalRecyclerView = (VerticalRecyclerView) findViewById(R.id.horizontalRecyclerView);
        verticalRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(5, OrientationHelper.VERTICAL));
        verticalRecyclerView.setAdapter(appAdapter);
        appAdapter.setOnItemClickLitener(this);
    }

    @Override
    public int initLayout() {
        return R.layout.activity_allapp;
    }

    private List<AppBean> getAllApp() {
        PackageManager localPackageManager = getPackageManager();
        Intent localIntent = new Intent(Intent.ACTION_MAIN);
        localIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        List<ResolveInfo> localList = localPackageManager.queryIntentActivities(localIntent, 0);
        Log.d("showboom", "localList " + localList.size());
        List<AppBean> data = new ArrayList<>();
        AppBean appBean;
        for(ResolveInfo app : localList) {
            appBean = new AppBean();
            appBean.setName(app.activityInfo.loadLabel(getPackageManager()).toString());
            appBean.setIcon(app.activityInfo.loadIcon(getPackageManager()));
            appBean.setPackageName(app.activityInfo.packageName);
            appBean.setClassName(app.activityInfo.name);
            data.add(appBean);
        }

        return data;
    }


    @Override
    public void onItemClick(View view, Object data, int position) {
        AppBean appBean = (AppBean)data;
        if(checkAppInstalled(appBean.getPackageName())) {
            Intent intent = new Intent();
            intent.setClassName(appBean.getPackageName(), appBean.getClassName());
            startActivity(intent);
        } else {
            Toast.makeText(AllAppActivity.this, "请检查应用", Toast.LENGTH_SHORT).show();
        }
    }

    public boolean checkAppInstalled(String packageName) {
        Intent intent = getPackageManager().getLaunchIntentForPackage(packageName);
        return intent == null ? false : true;
    }
}
